package com.test.qpbgboot.backend.controller;

import com.test.qpbgboot.backend.pojo.dto.MerchantAccountLogQuery;
import com.test.qpbgboot.backend.pojo.dto.MerchantQuery;
import com.test.qpbgboot.backend.pojo.dto.MerchantSaveParam;
import com.test.qpbgboot.backend.pojo.vo.MerchantAccountLogVO;
import com.test.qpbgboot.backend.pojo.vo.MerchantVO;
import com.test.qpbgboot.backend.service.MerchantAccountService;
import com.test.qpbgboot.backend.service.MerchantService;
import com.test.qpbgboot.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/v1/backend/merchant")
@Slf4j
@Api(tags = "backend module")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantAccountService merchantAccountService;

    @GetMapping("/select")
    @ApiOperation(value = "商家查询")
    public JsonResult selectMerchant(
            @Valid @ModelAttribute MerchantQuery merchantQuery) {
        log.debug("商家查询:merchantQuery={}", merchantQuery);
        List<MerchantVO> merchantVOList = merchantService.merchantSelect(merchantQuery);
        return JsonResult.success(merchantVOList);
    }

    @PostMapping("/save")
    @ApiOperation(value = "商家保存")
    public JsonResult saveMerchant(
            @Valid @RequestBody MerchantSaveParam merchantSaveParam) {
        log.debug("商家保存:merchantSaveParam={}", merchantSaveParam);
        merchantService.merchantSave(merchantSaveParam);
        return JsonResult.success();
    }

    @PostMapping("/log-off/{merchantId}")
    @ApiOperation(value = "商家注销")
    public JsonResult logOffMerchant(@PathVariable @ApiParam(value = "商家id",
            name = "merchantId", required = true) Long merchantId) {
        log.debug("商家注销:merchantId={}", merchantId);
        merchantService.merchantLogOff(merchantId);
        return JsonResult.success();
    }

    @GetMapping("/account-log/select")
    @ApiOperation(value = "商家账户记录查询")
    public JsonResult selectMerchantAccountLog(
            @Valid @ModelAttribute MerchantAccountLogQuery merchantAccountLogQuery) {
        log.debug("商家账户记录查询:merchantAccountLogQuery={}", merchantAccountLogQuery);
        List<MerchantAccountLogVO> accountLogVOList =
                merchantAccountService.merchantAccountLogSelect(merchantAccountLogQuery);
        return JsonResult.success(accountLogVOList);
    }
}
